package com.quxuecx.springcloud.controller;


import com.netflix.loadbalancer.IRule;
import com.quxuecx.springcloud.entities.CommonResult;
import com.quxuecx.springcloud.entities.Payment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: shaofeer
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: shaofeer@163.com
 * <p>
 * @time: 2020/6/28
 */
@RestController
@RequestMapping("/consumer/order")
@Slf4j
public class OrderController {

    private static final String PAYMENT_SERVICE_URL = "http://CLOUD-PAYMENT-SERVICE/service/payment";
    @Autowired
    private RestTemplate mRestTemplate;


    /**
     * 保存
     *
     * @param payment
     * @return
     */
    @PostMapping("/save")
    public CommonResult save(Payment payment) {
        return mRestTemplate.postForObject(PAYMENT_SERVICE_URL + "/save", payment, CommonResult.class);
    }

    /**
     * 根据id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id) {
        return mRestTemplate.getForObject(PAYMENT_SERVICE_URL + "/get/" + id, CommonResult.class);
    }

    /**
     * 根据id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/getByEntity/{id}")
    public CommonResult<Payment> getByIdByEntity(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> result = mRestTemplate.getForEntity(PAYMENT_SERVICE_URL + "/get/" + id, CommonResult.class);
        HttpStatus statusCode = result.getStatusCode();
        int statusCodeValue = result.getStatusCodeValue();
        log.info("OrderController  getByIdByEntity()   statusCodeValue   " + statusCodeValue);


        CommonResult<Payment> body = result.getBody();


        return body;
    }


}
